package com.meitun.mama.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meitun.mama.inject.Injector;
import com.meitun.mama.util.Config;

/**
 * Created by sunny01 on 2016/3/27.
 */
public abstract class BaseFragment extends Fragment {

    final String TAG = this.getClass().getSimpleName();

    private View root;

    protected abstract int getLayoutId();

    protected void intitBundle(Bundle savedInstanceState) {
    }
    protected void initView() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Config.isDebug) {
            Log.e(TAG, "onCreate " + savedInstanceState);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            intitBundle(getArguments());
        } else {
            Injector.onRestore(this, savedInstanceState);
        }
        if (Config.isDebug) {
            Log.e(TAG, "onCreateView " + savedInstanceState + "|" + root);
        }
        if (root == null) {
            root = inflater.inflate(getLayoutId(), container, false);
        }

        Injector.injectFragment(this, root);

        return root;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (Config.isDebug) {
            Log.e(TAG, "onSaveInstanceState " + outState);
        }
        Injector.save(this, outState);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (Config.isDebug) {
            Log.e(TAG, "onStart ");
        }
    }

    public void onResume() {
        super.onResume();
        if (Config.isDebug) {
            Log.e(TAG, "onResume ");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (Config.isDebug) {
            Log.e(TAG, "onPause ");
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (Config.isDebug) {
            Log.e(TAG, "onStop ");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Config.isDebug) {
            Log.e(TAG, "onDestroy ");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (Config.isDebug) {
            Log.e(TAG, "onDestroyView ");
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        if (Config.isDebug) {
            Log.e(TAG, "onViewCreated "+savedInstanceState);
        }
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (Config.isDebug) {
            Log.e(TAG, "onViewStateRestored "+savedInstanceState);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (Config.isDebug) {
            Log.e(TAG, "onActivityCreated "+savedInstanceState);
        }
    }

//    @Override
//    public void onAttach(Context context) {
//        super.onAttach(context);
//        if (Config.isDebug) {
//            Log.e(TAG, "onAttach context:"+context);
//        }
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Config.isDebug) {
            Log.e(TAG, "onActivityResult ");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (Config.isDebug) {
            Log.e(TAG, "onDetach ");
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (Config.isDebug) {
            Log.e(TAG, "onHiddenChanged "+hidden);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (Config.isDebug) {
            Log.e(TAG, "onAttach activity");
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (Config.isDebug) {
            Log.e(TAG, "setUserVisibleHint " + isVisibleToUser);
        }
    }
}
